from django.conf import settings
from django.template.loader import get_template
from django.core.mail import EmailMultiAlternatives


def send_appointment_reminder_email(user, appointments):
    from_email = settings.CONTACT_EMAIL
    to = user.email
    for appointment in appointments:

        subject = 'Appointment Reminder'
        html_template = get_template('reminder/appointment_reminder.html')
        context = {
            'full_name_receiver': user.first_name + ' ' + user.last_name,
            'date': appointment.start_date,
            'time': appointment.start_time
        }
        html_content = html_template.render(context)

        msg = EmailMultiAlternatives(subject=subject, from_email=from_email, to=[to])
        msg.attach_alternative(html_content, "text/html")

        try:
            msg.send()
            appointment.is_sent = True
            appointment.save()
        except Exception as ex:
            print(ex)


def send_client_notes_reminder_email(user, clients):
    from_email = settings.CONTACT_EMAIL
    to = user.email
    for client in clients:

        subject = 'Client Notes Reminder'
        html_template = get_template('reminder/client_notes_reminder.html')
        context = {
            'full_name_receiver': user.first_name + ' ' + user.last_name,
            'client_full_name': client.first_name + ' ' + client.last_name
        }
        html_content = html_template.render(context)

        msg = EmailMultiAlternatives(subject=subject, from_email=from_email, to=[to])
        msg.attach_alternative(html_content, "text/html")

        try:
            msg.send()
        except Exception as ex:
            print(ex)


def send_client_appointment_email_reminder(user, appointments):
    from_email = settings.CONTACT_EMAIL
    to = user.email
    for appointment in appointments:

        subject = 'Appointment Reminder'
        html_template = get_template('reminder/appointment_reminder.html')
        context = {
            'full_name_receiver': user.first_name + ' ' + user.last_name,
            'date': appointment.start_date,
            'time': appointment.start_time
        }
        html_content = html_template.render(context)

        msg = EmailMultiAlternatives(subject=subject, from_email=from_email, to=[to])
        msg.attach_alternative(html_content, "text/html")

        try:
            msg.send()
        except Exception as ex:
            print(ex)


def send_billing_reminder_email(user, invoice):
    from_email = settings.CONTACT_EMAIL
    to = user.email

    subject = 'Appointment Reminder'
    html_template = get_template('reminder/billing_reminder.html')
    context = {
            'full_name_receiver': user.first_name + ' ' + user.last_name,
            'due_date': invoice.due_date,
            'invoice_no': invoice.invoice_number
        }
    html_content = html_template.render(context)

    msg = EmailMultiAlternatives(subject=subject, from_email=from_email, to=[to])
    msg.attach_alternative(html_content, "text/html")

    try:
        msg.send()
    except Exception as ex:
        print(ex)
